<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <div>
      <div style="margin: 0 0 10px;float: left;">
        <router-link :to="{name: addLinkName}">
          <el-button type="primary" plain>添加</el-button>
        </router-link>
        <el-button type="danger" plain @click="delObjs()">删除</el-button>
      </div>

      <div style=" float: right;">
        <el-input placeholder="请输入标题" v-model="inputObjName" class="input-with-select">
          <el-button slot="append" icon="el-icon-search" @click="inputFunc()"></el-button>
        </el-input>
      </div>

      <div>
        <!--内容组件-->
        <el-table
          ref="multipleTable"
          :data="tableData"
          style="width: 100%;font-size: 12px"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange">

          <el-table-column
            type="selection"
            width="50">
          </el-table-column>

          <el-table-column
            prop="id"
            label="ID"
            width="60">
          </el-table-column>

          <el-table-column
            fixed="left"
            prop="title"
            label="标题"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.title">
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="obj_key"
            label="KEY"
            width="200">
          </el-table-column>

          <el-table-column
            label="关联接口"
            width="200">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>接口ID: {{ scope.row.api_info.id }}</p>
                <p>接口名称: {{ scope.row.api_info.name }}</p>
                <p>接口url: {{ scope.row.api_info.url }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.api_info.name }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            label="请求体sql"
            width="150">
            <template slot-scope="scope">
              <div v-if="scope.row.sql_body_info">
                <router-link target="_blank"
                             :to="{name: 'sqlEdit', query: { sqldetail_id: scope.row.sql_body_info.id }}">
                  <el-button type="text" size="small">{{ scope.row.sql_body_info.id }}:{{
                    scope.row.sql_body_info.sql_title }}
                  </el-button>
                </router-link>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="断言sql"
            width="150">
            <template slot-scope="scope">
              <div v-if="scope.row.sql_assert_info">
                <router-link target="_blank"
                             :to="{name: 'sqlEdit', query: { sqldetail_id: scope.row.sql_assert_info.id }}">
                  <el-button type="text" size="small">{{ scope.row.sql_assert_info.id }}:{{
                    scope.row.sql_assert_info.sql_title }}
                  </el-button>
                </router-link>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="步骤数据"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.steps_data">
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="URL"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.url">
              </div>
            </template>
          </el-table-column>

          <el-table-column prop="api_type" label="请求方式" width="120">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.api_type" disabled placeholder="请选择类型">
                <el-option
                  v-for="(item,index) in optionsApiType"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column
            prop="header_template"
            label="请求头模板"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.header_template"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="request_sql_template"
            label="请求sql模板"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.request_sql_template"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="request_pull_template"
            label="请求体模板"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.request_pull_template"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="assert_sql_template"
            label="断言sql模板"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.assert_sql_template"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="assert_pull_template"
            label="断言模板"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.assert_pull_template"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="header"
            label="请求头数据"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.header"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="body"
            label="请求体数据"
            width="200">
            <template slot-scope="scope">
              <div v-show="scope.row.body">
                <el-button type="text" size="small" @click="downTemplate(scope.row.body)">下载</el-button>
                <el-link type="success" :href="getUrl(scope.row.body)" target="_blank">查看</el-link>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="assert_data"
            label="断言数据"
            width="200">
            <template slot-scope="scope">
              <div v-show="scope.row.assert_data">
                <el-button type="text" size="small" @click="downTemplate(scope.row.assert_data)">下载</el-button>
                <el-link type="success" :href="getUrl(scope.row.assert_data)" target="_blank">查看</el-link>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="response"
            label="响应数据"
            width="200">
            <template slot-scope="scope">
              <div v-show="scope.row.response">
                <el-button type="text" size="small" @click="downTemplate(scope.row.response)">下载</el-button>
                <el-link type="success" :href="getUrl(scope.row.response)" target="_blank">查看
                </el-link>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="result_data"
            label="对比结果"
            width="200">
            <template slot-scope="scope">
              <div v-show="scope.row.result_data">
                <el-button type="text" size="small" @click="downTemplate(scope.row.result_data)">下载</el-button>
                <el-link type="success" :href="getUrl(scope.row.result_data)" target="_blank">查看</el-link>
              </div>
            </template>
          </el-table-column>

          <el-table-column prop="show_status" label="显示全部结果" width="120">
            <template slot-scope="scope">
              <el-switch
                disabled
                v-model="scope.row.show_status"
                active-text="是"
                inactive-text="否">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column fixed="right" prop="results" label="执行状态" width="120">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.results" disabled placeholder="执行情况">
                <el-option
                  v-for="(item,index) in optionsResults"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>


          <el-table-column prop="status" label="类型" width="120">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.status" disabled placeholder="请选择类型">
                <el-option
                  v-for="(item,index) in optionsCommon"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            label="操作"
            width="150">
            <template slot-scope="scope">
              <router-link :to="{name: EditLinkName, query: { apiDetail_id: scope.row.id }}">
                <el-button type="text" size="small">编辑</el-button>
              </router-link>
              <el-button type="text" size="small" @click="testObj(scope.row.id)">测试</el-button>
              <el-button type="text" size="small" :disabled="scope.row.status=1" @click="delObjs(scope.row.id)">删除</el-button>
              <!--<el-button type="text" size="small" @click="delObjs(scope.row.id)">删除</el-button>-->

            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页组件-->
      <div class="block paging">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          prev-text="上一页"
          next-text="下一页"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dataCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'ApiDetail',
    data() {
      return {
        addLinkName: 'ApiDetailAdd', // 添加的路由
        EditLinkName: 'ApiDetailEdit', // 编辑的路由
        // 需要搜索的接口名称
        inputObjName: '',  // 搜索功能实现的字符串
        // choices设置
        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        optionsApiType: [],  // 存储ApiTypeChoices实际数据的列表
        optionsResults: [],  // 执行状态

        // 分页设置
        currentPage: 1, //当前页码
        pageSize: 10,    //    每页的条数
        pageSizes: [10, 20, 30, 40],  // 可选每页显示条数
        dataCount: 0,  // 数据总条数

        // 数据设置
        ListDataURL: this.$http.apiDetailURL,  // 表格列表数据获取
        objID: 'apiDetail_id',
        tableData: [],  // 阿贾克斯获取的初始数据
        multipleSelection: [],  // 不能删，我也不知道是干嘛用的，删了会报错
      }
    },
    created() {
      // 发送啊假克丝请求
      this.objList();// 阿贾克斯获取的初始数据
      this.optionsCommon = this.getChoices.common;
      this.optionsApiType = this.getChoices.api.api_type;
      this.optionsResults = this.getChoices.results;

    },

    methods: {
      // 下载文件
      downTemplate(url) {
        this.$myTool.downTemplate(this, url)
      },
      // 跳转查看url
      getUrl(url) {
        return this.$http.HOST + url
      },

      // 测试sql功能
      testObj(objId) {
        let queryData = {
          id: objId
        };
        this.$http.getApiFunc(this.$http.getApiStatus)(this.$http.testApiDetailURL, queryData)
          .then(res => {
            this.$alert(res.data.msg, '提示', {
              confirmButtonText: '确定'
            });
            this.objList()
          })
          .catch(err => {
            console.log(err);
          });
      },

      handleClick(row) {
        console.log(row);
      },
      // 修改每页显示条数
      handleSizeChange(val) {
        this.pageSize = val;  // 修改每页显示条数
        this.currentPage = 1;  // 修改页码
      },
      // 修改页码
      handleCurrentChange(val) {
        this.currentPage = val;  // 修改页码
      },

      // 发送啊假克丝请求 得到表格的原始数据
      objList() {
        this.$myTool.objList(this)
      },
      // 删除函数，调用了删除接口
      delObjs(objId) {
        this.$myTool.delObjs(objId, this)
      },
      // 不能删
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      //  按项目名称搜索的函数
      inputFunc() {
        this.$myTool.inputFunc(this)
      },
    },

    watch: {
      'pageSize': function (value) {
        this.objList();
      },
      'currentPage': function (value) {
        this.objList();
      }
    },

    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },
  }
</script>

<style scoped>
  .paging {
    padding-top: 20px;
  }

  .mytable {
    /*width: 120px;*/
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }


</style>
